package com.news.servlet.admin;

import com.news.model.User;
import com.news.service.UserService;
import com.news.service.impl.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/userEdit")
public class UserEditServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            // 获取用户ID
            int userId = Integer.parseInt(request.getParameter("id"));
            
            // 获取用户信息
            User user = userService.findById(userId);
            if (user == null) {
                throw new Exception("用户不存在");
            }
            
            // 设置属性
            request.setAttribute("user", user);
            
            // 转发到编辑页面
            request.getRequestDispatcher("/admin/user_edit.jsp").forward(request, response);
        } catch (Exception e) {
            request.getSession().setAttribute("message", "获取用户信息失败：" + e.getMessage());
            response.sendRedirect(request.getContextPath() + "/admin/userManage");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            // 获取表单数据
            int userId = Integer.parseInt(request.getParameter("user_id"));
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String realname = request.getParameter("realname");
            String email = request.getParameter("email");
            String admin = request.getParameter("admin");

            // 获取原用户信息
            User user = userService.findById(userId);
            if (user == null) {
                throw new Exception("用户不存在");
            }

            // 更新用户信息
            user.setUsername(username);
            if (password != null && !password.trim().isEmpty()) {
                user.setPassword(password);
            }
            user.setRealname(realname);
            user.setEmail(email);
            if (userId != 1) {  // 不允许修改超级管理员的角色
                user.setAdmin(admin);
            }

            // 保存更新
            if (userService.updateUser(user)) {
                request.getSession().setAttribute("message", "用户更新成功！");
                response.sendRedirect(request.getContextPath() + "/admin/userManage");
            } else {
                request.getSession().setAttribute("message", "用户更新失败！");
                response.sendRedirect(request.getContextPath() + "/admin/userEdit?id=" + userId);
            }
        } catch (Exception e) {
            request.getSession().setAttribute("message", "用户更新失败：" + e.getMessage());
            response.sendRedirect(request.getContextPath() + "/admin/userManage");
        }
    }
} 